Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId: "",
    orderData: [],
    curOrderData: null,
    yearArr: [],
    index: 0,
    adminName:""
  },
  handleInput: function (e) {
    // let obj = {
    //   ...this.data.curOrderData
    // };
    // obj[e.currentTarget.dataset.param] = e.detail.value;
    // this.setData({
    //   curOrderData:obj
    // })
    this.setData({
      [`curOrderData.${e.currentTarget.dataset.param}`]: e.detail.value
    })
    if(e.currentTarget.dataset.param=="sellNum"){
      this.setData({
        'curOrderData.sellMoney': parseFloat(e.detail.value)*parseFloat(this.data.curOrderData.orderPrice)?parseFloat(e.detail.value)*parseFloat(this.data.curOrderData.orderPrice):""
      })
    }
  },
  bindDateChange: function(e) {
    console.log('选择的日期：', e.detail.value); // 输出格式：2025-07-23
    this.setData({
      'curOrderData.acquisitionDate': e.detail.value
    });
  },
  handleSubmit: async function (e) {
    let updateResult = await wx.cloud
    .callFunction({
      name: 'peasantServer',
      data: {
        type: 'updateRecord',
        data:{
          ...this.data.curOrderData,
          registrant:this.data.adminName
        }
      },
    });
    console.log(updateResult,"updateResult");
    if(updateResult.result.success){
      wx.showModal({
        title: '提示',
        content: '登记成功',
        showCancel: true,
        success: () => {
          this.backFun();
        }
      })
     
    }else{
      wx.showModal({
        title: '提示',
        content: '登记失败',
        showCancel: false
      })
    }
  },
  formatTime: function (date) {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0'); // 补零
    const day = String(date.getDate()).padStart(2, '0');
    // const hours = String(date.getHours()).padStart(2, '0');
    // const minutes = String(date.getMinutes()).padStart(2, '0');
    // const seconds = String(date.getSeconds()).padStart(2, '0');
    return `${year}-${month}-${day}`;
    // return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  },
  bindPickerChange: function (e) {
    this.setData({
      index: parseInt(e.detail.value),
      curOrderData: {
        ...this.data.orderData[parseInt(e.detail.value)],
        acquisitionDate: this.formatTime(new Date())
      }
    });
  },
  backFun: function () {
    wx.navigateBack({
      delta: 1 // 返回上一页，若设为2则返回上两页
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      userId: options.id
    });
    this.getOrderData(options.id);
    const that = this;
    // 异步读取
    wx.getStorage({
      key: 'adminName',
      success: res => {
        that.setData({
          adminName: res.data
        })
      },
      fail: err => console.error(err)
    });
  },
  async getOrderData(idNumber) {
    console.log(idNumber,"idNumber");
    wx.showLoading({
      title: '数据加载中...',
    });
    let orderData = await wx.cloud
      .callFunction({
        name: 'peasantServer',
        data: {
          type: 'queryOrderById',
          idNumber,
        },
      })
    wx.hideLoading()
    if (orderData.errMsg == "cloud.callFunction:ok") {
      let arr = orderData.result.data;
      arr.sort((a, b) => parseInt(b.year) - parseInt(a.year));
      let obj = {
        ...arr[0],
        acquisitionDate: this.formatTime(new Date())
      }
      console.log(obj,"obj");
      this.setData({
        orderData: arr,
        curOrderData: obj,
        yearArr: arr.map(a => a.year)
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})